﻿using System.Text;
using Register.IDAL;
using System.Data.SqlClient;
using System.Data;
using Register.Model;
using Register.DBUtility;
using System.Collections.Generic;
using System;
using Register;
namespace Register.DAL
{
    /// <summary>
    /// FileName: AdminInfoDAL.cs
    /// CLRVersion: 4.0.30319.269
    /// Author: WangYan
    /// Corporation: 
    /// Description: 
    /// DateTime: 2012/6/26 9:47:00
    /// </summary>
  public   class RoleDAL : IRole
    {
       string   tableName ="System_role";
        public List<Role> GetAllRole(string where, int pageIndex, int size)
        {
     
      
            DataSet  set=DbHelperSQL.getPagedData("*","Role_AddTime",where,tableName,OrderBy.DESC,pageIndex,size);
            List<Role> list=   ChangeReaderToList(set);  
  
            return list;
        }
        private List<Role> ChangeReaderToList(DataSet set)
        {
            List<Role> list = new List<Role>();
            DataTable table = set.Tables[0];
               foreach(DataRow row in  table.Rows )
                {
                    Role role = new Role();
                    role.Role_ID =(string )row["Role_ID"];
                    role.Role_Name = (string)row["Role_Name"];
                    string t=    row["Role_Describe"].ToString();
                   role.Role_Describe = t;
 
                    role.Role_Degree = (string)row["Role_Degree"];
                    role.Role_AddTime = (DateTime)row["Role_AddTime"];
                    role.Role_Recoder = (string)row["Role_Recoder"];
                    role.Role_LastAmendTime = (DateTime)row["Role_LastAmendTime"];
                    role.Role_LastAmendPerson = (string)row["Role_LastAmendPerson"];
                    if (role.Role_Describe.Length > 30)
                    {
                        role.Role_string = role.Role_Describe.Substring(0,25)+"........";
                    }
                    else
                    {
                        role.Role_string = t;
                    }
                     list.Add(role);
                }
            return list;
        }

        public int AddRole(Role role)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_role(");
            strSql.Append("Role_ID,Role_Name,Role_Describe,Role_Degree,Role_AddTime,Role_Recoder,Role_LastAmendTime,Role_LastAmendPerson)");
            strSql.Append(" values (");
            strSql.Append("@Role_ID,@Role_Name,@Role_Describe,@Role_Degree,@Role_AddTime,@Role_Recoder,@Role_LastAmendTime,@Role_LastAmendPerson)");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_ID", SqlDbType.Char,5),
					new SqlParameter("@Role_Name", SqlDbType.VarChar,20),
					new SqlParameter("@Role_Describe", SqlDbType.VarChar,100),
					new SqlParameter("@Role_Degree", SqlDbType.Char,1),
					new SqlParameter("@Role_AddTime", SqlDbType.DateTime),
					new SqlParameter("@Role_Recoder", SqlDbType.VarChar,20),
                    new SqlParameter("@Role_LastAmendTime",SqlDbType.DateTime),
                    new SqlParameter("@Role_LastAmendPerson",SqlDbType.VarChar,20)
				};
        
            parameters[0].Value = role.Role_ID;
            parameters[1].Value = role.Role_Name;
            parameters[2].Value = role.Role_Describe;
            if (role.Role_Describe == null)
                parameters[3].Value = System.DBNull.Value;
            else 
            parameters[3].Value = role.Role_Degree;    

            if (role.Role_Degree== null)
                parameters[4].Value = System.DBNull.Value;
            else
            parameters[4].Value = role.Role_AddTime;
            parameters[5].Value = role.Role_Recoder;
            parameters[6].Value = role.Role_LastAmendTime;
            parameters[7].Value = role.Role_LastAmendPerson;
         return    DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        public bool UpdateRole(Role role)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_role set ");
            strSql.Append("Role_Name=@Role_Name,");
            strSql.Append("Role_Describe=@Role_Describe,");
            strSql.Append("Role_Degree=@Role_Degree,");
            //strSql.Append("Role_AddTime=@Role_AddTime,");
            //strSql.Append("Role_Recoder=@Role_Recoder,");
            strSql.Append("Role_LastAmendTime=@Role_LastAmendTime,");
            strSql.Append("Role_LastAmendPerson=@Role_LastAmendPerson");
            strSql.Append(" where Role_ID=@Role_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_Name", SqlDbType.VarChar,20),
					new SqlParameter("@Role_Describe", SqlDbType.VarChar,100),
					new SqlParameter("@Role_Degree", SqlDbType.Char,1),
                    //new SqlParameter("@Role_AddTime", SqlDbType.DateTime),
                    //new SqlParameter("@Role_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@Role_LastAmendTime", SqlDbType.DateTime),
					new SqlParameter("@Role_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@Role_ID", SqlDbType.Char,5)};
            parameters[0].Value = role.Role_Name;
            parameters[1].Value = role.Role_Describe;
            parameters[2].Value = role.Role_Degree;
            //parameters[3].Value = role.Role_AddTime;
            //parameters[4].Value = role.Role_Recoder;
            parameters[3].Value = role.Role_LastAmendTime;
            parameters[4].Value = role.Role_LastAmendPerson;
            parameters[5].Value = role.Role_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool DeleteRole(string Role_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_role ");
            strSql.Append(" where Role_ID=@Role_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Role_ID", SqlDbType.Char,5)};
            parameters[0].Value = Role_ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetRoleCount(string where)
        {
            int count = 0;
            StringBuilder str = new StringBuilder();
            str.Append(" select count(*) from ");
            str.Append(tableName);
            if (where != null)
            {
                str.Append(" where ");
                str.Append(where);
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            else
            {
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            return count;
        }
    }
}
